package com.sh.suhe.utildemos.views.activity;

import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.sh.suhe.utildemos.R;
import com.sh.suhe.utildemos.surfaceview.SurfaceViewAnimation;
import com.sh.suhe.utildemos.utils.DialogUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 逐帧播放动画
 */
public class SurfaceAnimationActivity extends AppCompatActivity {
    @BindView(R.id.surfaceAnimaion)
    SurfaceViewAnimation surfaceAnimaion;

    private int[] mFrames_mvw; // 帧数组
    private int[] mFrames_listen; // 帧数组
    private int[] mFrames_think; // 帧数组

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_surface_animation);
        ButterKnife.bind(this);
        mFrames_listen = getData(R.array.listen);
        mFrames_mvw = getData(R.array.mvw_first);
        mFrames_think = getData(R.array.think);

        surfaceAnimaion.setBitmapResoursID(mFrames_mvw);

    }

    @OnClick({R.id.mvw, R.id.think, R.id.listen})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.mvw:
                surfaceAnimaion.setBitmapResoursID(mFrames_mvw);
                break;
            case R.id.think:
                surfaceAnimaion.setBitmapResoursID(mFrames_think);
                break;
            case R.id.listen:
                surfaceAnimaion.setBitmapResoursID(mFrames_listen);
                break;
        }
    }

    /**
     * 从xml中读取帧数组
     *
     * @param resId
     * @return
     */
    public int[] getData(int resId) {
        TypedArray array = getResources().obtainTypedArray(resId);

        int len = array.length();
        int[] intArray = new int[array.length()];

        for (int i = 0; i < len; i++) {
            intArray[i] = array.getResourceId(i, 0);
        }
        array.recycle();
        return intArray;
    }
}
